import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class AddContactFrame extends JFrame{
	
	
	public String name;
	public String ip;
	public String portV;
	public String portA;
	public String caly;
	JCheckBox box;
	
	
	
	public AddContactFrame(){
		
		setTitle("Dodawanie kontaktu");
		setSize(400,200);
		
		final JTextField nameField = new JTextField(10);
		final JTextField ipField = new JTextField(15);
		final JTextField portVField = new JTextField(4);
		final JTextField portAField = new JTextField(4);
		
		portVField.setText("4444");
		portAField.setText("4448");
		portVField.setEnabled(false);
		portAField.setEnabled(false);
		
		
		JLabel nameLabel = new JLabel("Nazwa kontaktu :");
		nameLabel.setLabelFor(nameField);
		JLabel ipLabel = new JLabel("Adres IP kontaktu :");
		nameLabel.setLabelFor(ipField);
		JLabel portVLabel = new JLabel("Port video :");
		nameLabel.setLabelFor(portVField);
		JLabel portALabel = new JLabel("Port audio :");
		nameLabel.setLabelFor(portAField);
		
		box = new JCheckBox("Urzyj własnych portów");
		box.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent e) {
				if(box.isSelected()){
					portVField.setEnabled(true);
					portAField.setEnabled(true);
				}else{
					portVField.setText("4444");
					portAField.setText("4448");
					portVField.setEnabled(false);
					portAField.setEnabled(false);
					
				}
				
			}
			
		});
		
		
		JLabel boxLabel = new JLabel("");
		
		JButton button = new JButton("Dodaj");
		JButton cencel = new JButton("Anuluj");
		
		
		button.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				name = nameField.getText();
			System.out.println(name);
				ip = ipField.getText();
			System.out.println(name);
				portV = portVField.getText();
			System.out.println(name);
				portA = portAField.getText();

				System.out.println(name+ip+portV+portA);
				dispose();
			System.out.println(name+ip+portV+portA);
			
				 Komunikator.name = name;
				 Komunikator.ip = ip;
				 Komunikator.portV = portV;
	 			 Komunikator.portA = portA;
				 caly = name+"*"+ip+"/"+portV+"/"+portA;
				 Komunikator.caly=caly;
				
				Komunikator.zapiszFile();
				Komunikator.lista.addElement(caly);
				
				
			}

			
		});
		
		cencel.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent e) {
				dispose();
				
			}
			
		});
		
		JPanel panelField = new JPanel();
		panelField.add(nameLabel);
		panelField.add(nameField);
		panelField.add(ipLabel);
		panelField.add(ipField);
		
		panelField.add(box);
		panelField.add(boxLabel);
		panelField.add(portVLabel);
		panelField.add(portVField);
		panelField.add(portALabel);
		panelField.add(portAField);
		
		panelField.setLayout(new GridLayout(5,1));
		
		JPanel panelButton = new JPanel();
		
		
		panelButton.add(button, BorderLayout.LINE_END);
		panelButton.add(cencel, BorderLayout.AFTER_LINE_ENDS);
		
		getContentPane().add(panelField, BorderLayout.CENTER);
		getContentPane().add(panelButton, BorderLayout.SOUTH);
	

	}
	public String getNameC(){
		System.out.println("Imie to " + name );
		return name;
	}

}
